package eu.boss.listadapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import eu.boss.R;
import eu.boss.model.Player;

/**
 * Custom adapter to fill the custom listview items 
 * @author Arnaud
 *
 */
public class PlayerAdapter extends BaseAdapter{

	private List<Player> players;
	LayoutInflater inflater;
	Context context;
	
	public PlayerAdapter(Context c, List<Player> p){
		players=p;
		context=c;
		inflater=LayoutInflater.from(context);		
	}
	
	public int getCount() {
		return players.size();
	}

	public Object getItem(int position) {
		return players.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * kind of buffer to refresh the list
	 * @author Arnaud
	 *
	 */
	private class ViewHolder{
		TextView tvName;
		TextView tvRatio;
		TextView tvColor;
	}
	
	/**
	 * This method fill every single item included in the listview
	 */
	
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;

		if(convertView == null) {
			
		//initialize components
		holder = new ViewHolder();
		convertView = inflater.inflate(R.layout.listviewitemplayer, null);
		holder.tvName = (TextView)convertView.findViewById(R.id.tvName);
		holder.tvRatio = (TextView)convertView.findViewById(R.id.tvRatio);
		holder.tvColor = (TextView)convertView.findViewById(R.id.tvColor);
		convertView.setTag(holder);

		} else {

		holder = (ViewHolder) convertView.getTag();

		}

		//fill component
		holder.tvName.setText(players.get(position).getName());
		holder.tvRatio.setText(players.get(position).getRatio());
		holder.tvColor.setBackgroundDrawable(context.getResources().getDrawable(players.get(position).getColor()));

		return convertView;
	}

}
